//
//  ZHLEETabBarVC.swift
//  ZHLEEBlog_swift_pro
//
//  Created by ZHLEEBlander on 15/10/9.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEETabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabB = ZHLEETabBar()
        
        tabB.plusButtonBlock = {
        
            print("blockClick")
            
            //初始化composeView
            let composeView = ZHLEEComposeView()
            //显示
            composeView.show()
            
        }
        
        setValue(tabB, forKey: "tabBar")

//        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(ZHLEEHomeViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        addChildViewController(ZHLEEMessageViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        addChildViewController(ZHLEEDiscoverViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addChildViewController(ZHLEEProfileViewController(), title: "我", imageName: "tabbar_profile",index: 3)
    }
    private func addChildViewController(childController: UIViewController, title: String, imageName: String,index: Int) {
        
        let item = ZHLEETabBarItem()
        
        item.index = index
        
        childController.tabBarItem = item
        
        childController.title = title
        
        childController.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置文字颜色
        let arrt = [NSForegroundColorAttributeName:UIColor.orangeColor()]
        childController.tabBarItem.setTitleTextAttributes(arrt, forState: UIControlState.Selected)
        
        //初始化导航控制器
        let nav = ZHLEENavViewController(rootViewController: childController)
        
        addChildViewController(nav)
    }
    
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        let zhItem = item as! ZHLEETabBarItem
        var index = 0
        for tabBarChild in tabBar.subviews {
            
            if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
//                printLog()
                if index == zhItem.index{
                
                    for tabBarButtonChild in tabBarChild.subviews {
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            //拿到显示image的控件了
                            tabBarButtonChild.transform = CGAffineTransformMakeScale(0.8, 0.8)
                            
                            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                                tabBarButtonChild.transform = CGAffineTransformIdentity
                                
                                }, completion: { (finish) -> Void in
                                    
                            })
                        }
                    }
                }
                index++
            }
        }
    }
}
